import React, { useEffect } from 'react';
import { BrowserRouter as Router, Routes, Route, useNavigate } from "react-router-dom";
import DownloadPage from './pages/DownloadPage/DownloadPage';
import PromotionsPage from './pages/PromotionsPage/PromotionsPage';
import HomePage from './pages/HomePage/HomePage';
import UploadPage from './pages/UploadPage/UploadPage';
import AuthPage from './pages/AuthPage/AuthPage.jsx';
import ProfilePage from './pages/ProfilePage/ProfilePage.jsx';
import Navbar from './components/Navbar/Navbar.jsx';
import ShopPage from './pages/ShopPage/ShopPage.jsx';
import SearchPage from './pages/SearchPage/SearchPage';
import "./App.css";

function AppContent() {
    const navigate = useNavigate();

    // 如果没有登录，跳转到登录页面
    useEffect(() => {
        const isAuthenticated = localStorage.getItem('token');
        const currentPath = window.location.pathname;

        if (!isAuthenticated && currentPath !== '/auth') {
            navigate('/auth');
        }
    }, [navigate]);


    // 作弊检测函数
    const fetchCheatingCheck = async () => {
        const userId = localStorage.getItem("userId");
        const token = localStorage.getItem("token");
        
        if (!userId || !token) {
            console.log('用户未登录，跳过作弊检测');
            return;
        }

        try {
            const response = await fetch(`/api/cheating/check-all?userId=${userId}`, {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            });
            if (!response.ok) {
                throw new Error('作弊检测模块失败');
            }
            const data = await response.json();
            console.log(data.msg);
            if (!data.result) {
                window.alert(data.msg)
            } 
        } catch (error) {
            console.error('作弊检测模块失败:', error);
        } 
    };

    // 设置定时器，每2秒执行一次作弊检测
    useEffect(() => {
        fetchCheatingCheck();
        const intervalId = setInterval(fetchCheatingCheck, 2000);
        return () => clearInterval(intervalId);
    }, []); 

    return (
        <>
            <Navbar />
            <div className="App">
                <Routes>
                    <Route path="/" element={<HomePage />} />
                    <Route path="/auth" element={<AuthPage />} />
                    <Route path="/download" element={<DownloadPage />} />
                    <Route path="/promotions" element={<PromotionsPage />} />
                    <Route path="/upload" element={<UploadPage />} />
                    <Route path="/profile" element={<ProfilePage />} />
                    <Route path="/shop" element={<ShopPage />} />
                    <Route path="/search" element={<SearchPage />} />
                </Routes>
            </div>
            <footer className="app-footer">
                <div className="footer-container">
                    <div className="footer-links">
                        <span className="footer-link">
                            网站简介
                        </span>
                        <span className="footer-separator"></span>
                        <span className="footer-link">
                            关于我们
                        </span>
                        <span className="footer-separator"></span>
                        <span className="footer-link">
                            隐私政策
                        </span>
                        <span className="footer-separator"></span>
                        <span className="footer-link">
                            帮助中心
                        </span>
                        <span className="footer-separator"></span>
                        <span className="footer-link">
                            意见反馈
                        </span>
                    </div>

                    <div className="footer-copyright">
                        <p>Copyright © {new Date().getFullYear()} www.pt.com, All Rights Reserved.</p>
                        <p className="footer-contact">联系我们: contact@pt.com | 400-000-0000</p>
                    </div>
                </div>
            </footer>
        </>
    );
}

function App() {
    return (
        <Router>
            <AppContent />
        </Router>
    );
}

export default App;